<?php
namespace app\admin\controller;
use app\admin\model\News as NewsModel;
use app\admin\model\NewsCate;
use think\Controller;
use think\Db;
use think\facade\Request;
// die(Db::name("news_cate")->getLastSql());
class Common extends Controller
{
    protected $mod,$role,$system,$nav,$menudata,$cache_model,$categorys,$module,$moduleid,$adminRules,$HrefId;
    public function initialize()
    {
        //判断管理员是否登录
        if (!session('aid')) {
            $this->redirect('admin/login/index');
        }
        define('MODULE_NAME',strtolower(request()->controller()));
        define('ACTION_NAME',strtolower(request()->action()));
        //权限管理
        //当前操作权限ID
        if(session('aid')!=1){
            $this->HrefId = db('auth_rule')->where('href',MODULE_NAME.'/'.ACTION_NAME)->value('id');
            //当前管理员权限
            $map['a.admin_id'] = session('aid');
            $rules=Db::table(config('database.prefix').'admin')->alias('a')
                ->join(config('database.prefix').'auth_group ag','a.group_id = ag.group_id','left')
                ->where($map)
                ->value('ag.rules');
            $this->adminRules = explode(',',$rules);
            if($this->HrefId){
                if(!in_array($this->HrefId,$this->adminRules)){
                    $this->error('您无此操作权限');
                }
            }
        }
        $this->cache_model=array('AuthRule','System');
        foreach($this->cache_model as $r){
            if(!cache($r)){
                savecache($r);
            }
        }
        $this->system = cache('System');
        $this->rule = cache('AuthRule');
    }
    //空操作
    public function _empty(){
        return $this->error('空操作，返回上次访问页面中...');
    }

    /**
     * 获取分页参数
     * @param $page
     * @param $pageSize
     * @return mixed
     */
    public function getPageAndPageSize($page,$pageSize){
        $pageOption['page'] = $page?$page:1;;
        $pageOption['pageSize'] = $pageSize?$pageSize:config('pageSize');
        return $pageOption;
    }

    /**
     * 选择新闻作为轮播
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selectNews()
    {
        if (Request::isAjax()) {
            $title = trim(input("post.title"))?trim(input("post.title")):"";
            $cate_id = trim(input("post.cate_id"))?trim(input("post.cate_id")):0;

            // TODO:获取where条件
            $where = [];
            $where[] = ["b.title","like",'%'.$title.'%'];
            $where[] = ["b.event_image","<>",'-1'];
            $where[] = ["b.is_del","=",1];
            // $where[] = ["a.is_del","=",1];
            if($cate_id != 0)
            {
                $where[] = ["b.cate_id","=",$cate_id];
            }

            //获取分页参数
            $pageOption = $this->getPageAndPageSize(input('page'),input('limit'));

            //获取列表
            $field = "b.title,b.id,b.cate_name,b.event_image";
            $list = NewsModel::getPageListWithNewsCate($pageOption['page'],$pageOption['pageSize'],$where,['b.create_time'=>'desc'],$field);
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }

        // 获取栏目列表
        $field = "id,cate_name";
        $where[] = ["is_del",'=',1];
        $cate_list = NewsCate::getFieldValue($field,$where,true);
        $this->assign("cate_list",$cate_list);
        return $this->fetch("select_news");
    }

    /**
     * 头条选择
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selectNewsHead()
    {
        if (Request::isAjax()) {
            $title = trim(input("post.title"))?trim(input("post.title")):"";
            $cate_id = trim(input("post.cate_id"))?trim(input("post.cate_id")):0;

            // TODO:获取where条件
            $where = [];
            $where[] = ["b.title","like",'%'.$title.'%'];
            $where[] = ["b.is_del","=",1];
            // $where[] = ["a.is_del","=",1];
            if($cate_id != 0)
            {
                $where[] = ["b.cate_id","=",$cate_id];
            }

            //获取分页参数
            $pageOption = $this->getPageAndPageSize(input('page'),input('limit'));

            //获取列表
            $field = "b.title,b.id,b.cate_name,b.event_image";
            $list = NewsModel::getPageListWithNewsCate($pageOption['page'],$pageOption['pageSize'],$where,['b.create_time'=>'desc'],$field);
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        $headID = input("id");
        // 获取栏目列表
        $field = "id,cate_name";
        $where[] = ["is_del",'=',1];
        $cate_list = NewsCate::getFieldValue($field,$where,true);
        $this->assign("cate_list",$cate_list);
        $this->assign("headID",$headID);
        return $this->fetch("select_news_head");
    }

    /**
     * 手机号码验证
     * @param $mobile_phone
     * @return bool
     */
    function is_mobile_phone($mobile_phone){
        $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}|16[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/";
        if (preg_match($chars, $mobile_phone)) {
            return true;
        }
        return false;
    }
}
